package com.admgt.util.ftp;

import java.io.File;

import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.ssl.SslConfigurationFactory;
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;

public class FTPServerUtil {
	
	public static void basicServerUp() throws FtpException{
		FtpServerFactory serverFactory = new FtpServerFactory();
		FtpServer server = serverFactory.createServer();
		// start the server
		server.start();
		
	}
	
	public static void serverUpWithProtNumber() throws FtpException{
		FtpServerFactory serverFactory = new FtpServerFactory();
        
		ListenerFactory factory = new ListenerFactory();
		        
		// set the port of the listener
		factory.setPort(2221);

		// replace the default listener
		serverFactory.addListener("default", factory.createListener());
		                
		// start the server
		FtpServer server = serverFactory.createServer(); 
		        
		server.start();
	}
	
	public static void serverUpOverSSL() throws FtpException{
		FtpServerFactory serverFactory = new FtpServerFactory();
        
		ListenerFactory factory = new ListenerFactory();
		        
		// set the port of the listener
		factory.setPort(2221);

		// define SSL configuration
		SslConfigurationFactory ssl = new SslConfigurationFactory();
		ssl.setKeystoreFile(new File("/ftpserver.jks"));
		ssl.setKeystorePassword("password");

		// set the SSL configuration for the listener
		factory.setSslConfiguration(ssl.createSslConfiguration());
		factory.setImplicitSsl(true);

		// replace the default listener
		serverFactory.addListener("default", factory.createListener());
		        
		PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
		userManagerFactory.setFile(new File("/users.properties"));
		        
		serverFactory.setUserManager(userManagerFactory.createUserManager());
		        
		// start the server
		FtpServer server = serverFactory.createServer(); 
		        
		server.start();
	}

	public static void main(String[] args) {
		
		try {
			//FTPServerUtil.basicServerUp();
			//FTPServerUtil.serverUpWithProtNumber();
			FTPServerUtil.serverUpOverSSL();
		} catch (FtpException e) {
			e.printStackTrace();
		}
	}
}
